<template>
  <van-popup
    :value="value"
    @click-close-icon="$emit('input', false)"
    closeable
    position="left"
  >
   <div class="article-channel">
     <div class="head">
       <h3>我的频道<small>点击进入频道</small></h3>
        <a class="edit" :class="{active:isEditing===true}" href="javascript:;" @click="isEditing=!isEditing">{{isEditing?'完成':"编辑"}}</a>
      </div>
      <div class="body">
       <a href="javascript:;" @click="gotoCurrentChannel(index)" :class="{active: active===index}" v-for="(channel,index) in mychannels" :key="channel.id">
           {{channel.name}}
           <geek-icon @click.native.stop="deleteChannel(channel.id)" v-show="isEditing && index!==0 && index!== active" name="tag-close"></geek-icon>
    </a>
     </div>
      <div class="head" style="margin-top:12px">
        <h3>频道推荐</h3>
      </div>
     <div class="body">
        <a href="javascript:;" @click="addChannel(channel)" v-for="channel in optionChannels" :key="channel.id">+ {{channel.name}}</a>
      </div>
    </div>
  </van-popup>
</template>
<script>
import { getAllChannelList } from '@/api/channel'
import { addUserChannels, deleteChannel } from '@/api/user'
export default {
  name: 'ArticleChannel',
  data () {
    return {
      isEditing: false,
      allChannelList: []
    }
  },
  async created () {
    const [err, data] = await getAllChannelList()
    if (err) this.$toast.fail(err)
    this.allChannelList = data.data.channels
  },
  computed: {
    optionChannels () {
      return this.allChannelList.filter((item) => {
        return !this.mychannels.find(myitem => myitem.id === item.id)
      })
    }
  },
  methods: {
    // 删除频道
    async deleteChannel (id) {
      const [err] = await deleteChannel(id)
      if (err) return this.$toast.fail(err)

      // 操作父组件的数据
      const index = this.mychannels.findIndex((item) => { return item.id === id })
      this.mychannels.splice(index, 1)
    },
    async addChannel (channel) {
      const channels = []
      this.mychannels.forEach((item, index) => {
        channels.push({
          id: item.id,
          name: item.name,
          seq: index + 1
        })
      })

      channels.push({
        id: channel.id,
        name: channel.name,
        seq: channels[channels.length - 1].seq + 1
      })
      // 添加频道
      await addUserChannels(channels)
      // 成功了
      // 将点击到频道 添加到 mychannels
      this.mychannels.push(channel)
    },
    gotoCurrentChannel (index) {
      //   1.隐藏弹出层
      this.$emit('input', false)
      //   2.将数据传递给父组件
      this.$emit('updateIndex', index)
    }
  },
  props: {
    active: {
      type: Number,
      default: 0
    },
    mychannels: {
      type: Array,
      default: () => []
    },
    value: {
      type: Boolean,
      default: false
    }
  }
}
</script>
<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
}
.article-channel {
  margin-top: 44px;
}

.article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #DE644B;
      border-radius: 11px;
      border: 1px solid #DE644B;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #DE644B;
      }
    }
  }
  .body {
    padding: 5px 6px 0 16px;
    a {
        position: relative;
        .geek-icon {
            position: absolute;
            top: -5px;
            right: -5px;
            line-height: 1;
        }
      display: inline-block;
      padding: 0 8px;
      font-size:14px;
      color: #3A3948;
      background: #F7F8FA;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
      &.active {
        color: @geek-color;
      }
    }
  }
}
</style>
